
package com.wyzz.hr.action;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.bo.IClientBO;
import com.wyzz.hr.bo.LogBo;
import com.wyzz.hr.bo.UserBo;
import com.wyzz.hr.pojo.Client;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.spring.SpringBeanFactory;

public class LoginAction extends BaseAction {

	public LoginAction() {
		uiPassword = null;
		checkCode = null;
		macAddress = null;
	}

	public String loginExecute() throws Exception {
		if (user == null || user.getUiUsername() == null) {
			addActionError("\u7528\u6237\u521D\u59CB\u5316\u51FA\u9519\uFF01");
			return "error";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		String savedCode = (String) session.getAttribute("check_code");
		if (savedCode == null || !savedCode.equalsIgnoreCase(checkCode)) {
			addActionError("\u9A8C\u8BC1\u7801\u9519\u8BEF\uFF01");
			return "error";
		}
		UserBo userBo = (UserBo) SpringBeanFactory.getBean("userService");
		String message = userBo.doLogin(user, uiPassword, macAddress);
		if (message.equals("yes")) {
			updateLoggers();
			return "success";
		} else {
			addActionError(message);
			return "error";
		}
	}

	private void updateLoggers() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		String info = (new StringBuilder()).append(
				"\u767B\u5F55\u65F6\u95F4\uFF1A").append(df.format(new Date()))// 登录时间
																				// ：
				.append("  \u7528\u6237\u540D\uFF1A").append(// 用户名：
						user.getUiUsername()).append("  \u767B\u5F55IP\uFF1A")// 登录IP
																				// ：
				.append(user.getUiLastLoginIp()).toString();
		LogBo logBo = (LogBo) SpringBeanFactory.getBean("logmanager");
		logBo.updateXML((new StringBuilder()).append(
				FileOperate.getFileHomePath()).append("login_log/login.xml")
				.toString(), (new StringBuilder()).append(info).append("\n")
				.toString());
	}

	public String getMac() {
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.substring(uuid.lastIndexOf('-') + 1);
		char macChar[] = new char[17];
		int j = 0;
		int k = 0;
		for (int i = 0; i < 17; i++)
			if (++j % 3 == 0) {
				macChar[i] = '-';
			} else {
				macChar[i] = uuid.charAt(k);
				k++;
			}

		return new String(macChar);
	}

	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	public String getUiPassword() {
		return uiPassword;
	}

	public void setUiPassword(String uiPassword) {
		this.uiPassword = uiPassword;
	}

	public String getCheckCode() {
		return checkCode;
	}

	public void setCheckCode(String checkCode) {
		this.checkCode = checkCode;
	}

	public String getMacAddress() {
		return macAddress;
	}

	public void setMacAddress(String macAddress) {
		this.macAddress = macAddress;
	}

	private static final long serialVersionUID = 1L;
	private Userinfo user;
	private String uiPassword;
	private String checkCode;
	private String macAddress;
	static Logger logger = Logger
			.getLogger(com.wyzz.hr.action.LoginAction.class);

}
